package com.cssl.springbootdemo01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cssl.springbootdemo01.dao.UsersDao;
import com.cssl.springbootdemo01.dao.UsersMongoRepository;
import com.cssl.springbootdemo01.pojo.Users;
import com.cssl.springbootdemo01.service.UsersService;
import com.cssl.springbootdemo01.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service
public class UsersServiceImpl extends ServiceImpl<UsersDao,Users> implements UsersService {

    @Resource
    private UsersDao uDao;

    @Autowired
    private RedisUtil util;

    @Autowired
    private UsersMongoRepository usersMongoRepository;

    @Override
    public List<Users> select() {
        System.out.println("uDao");
        return uDao.select();
    }

    /*
    *查所有 redisTemplate
     */
    @Override
    public List<Users> find() {
        List<Users> list=null;
        if (util.exists("list")){
            System.out.println("存在");
            list=(List<Users>)util.get("list");
        }else{
            System.out.println("不存在");
            list=super.list();
            util.set("list",list,60L);
        }
        return list;
    }

    /**
     * 增加
     * @param id
     * @return
     */
    public boolean insert(int id){
        boolean id1=false;
        if (util.exists("id")){
            System.out.println("存在");
            id=(int)util.get("id");
            System.out.println(id);
        }else{
            System.out.println("不存在");
             id1= util.set("id", id,60L);
        }
        System.out.println(id);
        return id1;
    }

    @Override
    public boolean delete(int id) {
        boolean id1=false;
        if (util.exists("id")){
            System.out.println("存在-删除");
            util.remove("id");
            id1=true;
        }else{
            System.out.println("不存在-增加");
            util.set("id", id,60L);
        }
        return id1;
    }

    @Override
    public boolean update(int id) {
        boolean id1=false;
        if (util.exists("id")){
            System.out.println("存在-修改");
            util.set("id", id,60L);
            id1=true;
        }else{
            System.out.println("不存在-增加");
            util.set("id", id,60L);
            id1=true;
        }
        return id1;
    }

    /**
     * 查询方法
     * 以key(userid)方法返回对象为value保存到redis中
     * 存在直接去redis值返回，不存在则调用该方法，同时将返回值保存到redis
     * @param id
     * @return
     */
   /* @Cacheable(value="user", key="#id", unless="#result==null",
            condition="#id%2==0", cacheManager = "cacheManager2")
    @Override
    public Users findByid(int id) {
        //System.out.println("users"+super.getById(id));
        List<Users>  list=super.list();
        for (Users users : list) {
            System.out.println(users);
        }
        Users users = super.getOne(new QueryWrapper<Users>().eq("uid", id));
        System.out.println(users);
        return users;
    }*/


    @CacheEvict(value = "user", key = "#id", condition = "#id%2==0")
    @Override
    public boolean eDelete(int id){
        System.out.println("delete:"+id+"/"+super.removeById(id));
        return super.remove(new QueryWrapper<Users>().eq("uid",id));
    }

    @CachePut(value = "user", key = "#id")
    @Override
    public Users findByid(int id) {
        List<Users> list=super.list();
        for (Users users : list) {
            System.out.println(users);
        }
        Users users = super.getOne(new QueryWrapper<Users>().eq("uid", id));
        System.out.println(users);
        return users;
    }

    //mongodb
    public List<Users> findByUname(String username){
       return usersMongoRepository.findByUname(username);
    }

    @Override
    public Users saveUsers(Users users) {
        return usersMongoRepository.save(users);
    }

    @Override
    public List<Users> findAll() {
        return usersMongoRepository.findAll();
    }

    @Override
    public void removeUsers(int uid) {
        usersMongoRepository.deleteById(uid);
    }


}
